from django.shortcuts import render
# Create your views here.
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.views import APIView
from rest_framework_jwt.authentication import jwt_decode_handler, JSONWebTokenAuthentication

from ES_utils.es_utils import getClient, return_filter
from app.models import *
from app.utils import *
import json
# Create your views here.
@method_decorator(csrf_exempt, name="dispatch")
class ManageClaimAuthorView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        data  = []
        claims = Claim.objects.filter()
        for claim in claims:
            data.append({
                'user_id':claim.uid,
                'claim_id':claim.id,
                'author_id':claim.aid,
                'image_url':claim.image_url,
                'text':claim.text,
                'time':claim.time
            })
        return JsonResponse(
        {
            'isSuccess': '0',
            'errormsg': '返回认领学者成功',
            'data': data
        }
    )

@method_decorator(csrf_exempt, name="dispatch")
class GetClaimAuthorView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        claim_id = request.data.get("claim_id")
        claim = Claim.objects.get(id=claim_id)
        claim.delete()
        flag = request.data.get("flag")
        user_id = claim.uid
        user = User.objects.get(id=user_id)
        es_id = claim.aid
        if flag == "1":
            user.aid = es_id
            user.scholar = 1
            user.save()
        return JsonResponse(
        {
            'isSuccess': '0',
            'errormsg': '获取认领学者反馈成功',
        }
    )

@method_decorator(csrf_exempt, name="dispatch")
class ManageMissingPaperView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        data = []
        missing_papers = Missingpaper.objects.filter()
        for missingpaper in missing_papers:
            data.append({
                'missingpaper_id':missingpaper.id,
                'user_id':missingpaper.uid.id,
                'title':missingpaper.title,
                'author':missingpaper.author,
                'keyword':missingpaper.keyword,
                'date':missingpaper.date,
                'doi':missingpaper.doi,
                'abstract':missingpaper.abstract,
                'link':missingpaper.link,
                'time':missingpaper.date
            })
        return JsonResponse(
            {
                'isSuccess': '0',
                'errormsg': '返回缺失文献成功',
                'data' : data
            }
        )

@method_decorator(csrf_exempt, name="dispatch")
class GetMissingPaperView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        missingpaper_id = request.data.get("missingpaper_id")
        missingpaper = Missingpaper.objects.get(id = missingpaper_id)
        missingpaper.delete()
        return JsonResponse(
            {
                'isSuccess': '0',
                'errormsg': '已收到管理文献信息',
            }
        )

@method_decorator(csrf_exempt, name="dispatch")
class ManagePaperComplainView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        data = []
        complains = Complain.objects.filter()
        for complain in complains:
            data.append({
                'paper_id':complain.pid,
                'complain_id':complain.id,
                'image_url':complain.image_url,
                'introduction':complain.introduction,
                'contact':complain.contact,
                'time':complain.time
            })
        return JsonResponse(
            {
                'isSuccess': '0',
                'errormsg': '返回文献申诉成功',
                'data':data
            }
        )

@method_decorator(csrf_exempt, name="dispatch")
class GetPaperComplainView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        complain_id = request.data.get("complain_id")
        complain = Complain.objects.get(id = complain_id)
        complain.delete()
        return JsonResponse(
            {
                'isSuccess': '0',
                'errormsg': '已收到管理文献申诉信息',
            }
        )

@method_decorator(csrf_exempt, name="dispatch")
class ManageTipOffView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        comments = Comment.objects.filter(is_reported=True)
        notes = Note.objects.filter(is_reported=True)
        comment_data = []
        for comment in comments:
            comment_data.append({
                'sender_id': comment.uid_0.id,
                'time': comment.time,
                'content': comment.content,
                'remark_id':comment.id
            })
        note_data = []
        for note in notes:
            note_data.append({
                'time':note.time,
                'author_id':note.uid.id,
                'note_id':note.id,
                'introductin':note.introduction
            })
        return JsonResponse(
            {
                'isSuccess': '0',
                'errormsg': '返回举报信息成功',
                'comment':comment_data,
                'note':note_data
            }
        )

@method_decorator(csrf_exempt, name="dispatch")
class GetTipOffView(APIView):
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [AllowAny]
    def post(self, request):
        comment_id =request.data.get("comment_id")
        note_id = request.data.get("note_id")
        flag = request.data.get("flag")
        if comment_id == "":
            note = Note.objects.get(id = note_id)
            note.is_reported = False
            note.save()
            if flag == "1":
                note.delete()
        if note_id == "":
            comment = Comment.objects.get(id = comment_id)
            comment.is_reported = False
            comment.save()
            if flag == "1":
                comment.delete()
        return JsonResponse(
            {
                'isSuccess': '0',
                'errormsg': '已收到举报反馈信息',
            }
        )